/**
 * 
 */
package com.java.test.audit.mapper;

import com.java.test.audit.dto.DepositExamineInfo;
import com.java.test.user.dto.PlayAccountInfo;
import com.java.test.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 存款审核
 *
 */
@Component
public class DepositExamineMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int selectCount(DepositExamineInfo info) {
		String sql = "select count(*) from examine_deposit where 1=1 ";

		if (info.getDepositTime() != null) {
			sql += " and deposit_time>='" + info.getDepositTime() + "'";
		}

		if (info.getMakecollections() != null && !"".equals(info.getMakecollections())) {
			sql += " and make_collections in (" + info.getMakecollections() + ")";
		}
		if (info.getSliceValue() != 0) {
			sql += " and slice_value ='" + info.getSliceValue() + "'";
		}

		if (info.getGameAccount() != null && !"".equals(info.getGameAccount())) {
			sql += " and game_account like '%" + info.getGameAccount() + "%'";
		}

		Integer i = jdbcTemplate.queryForObject(sql, Integer.class);

		return i == null ? 0 : i;

	}

	public List<DepositExamineInfo> selectDepositExamineList(PageInfo<DepositExamineInfo> page,
			DepositExamineInfo info) {
		String sql = "select * from examine_deposit where 1=1 ";

		if (info.getDepositTime() != null) {
			sql += " and deposit_time>='" + info.getDepositTime() + "'";
		}

		if (info.getMakecollections() != null && !"".equals(info.getMakecollections())) {
			sql += " and make_collections in (" + info.getMakecollections() + ")";
		}

		if (info.getSliceValue() != 0) {
			sql += " and slice_value ='" + info.getSliceValue() + "'";
		}

		if (info.getGameAccount() != null && !"".equals(info.getGameAccount())) {
			sql += " and game_account like '%" + info.getGameAccount() + "%'";
		}
		int start = (page.getCurrentPage() - 1) * page.getPageCount();
		int limit = page.getPageCount();

		sql += " order by deposit_time desc limit ?,?";
		List<DepositExamineInfo> list = jdbcTemplate.query(sql, new DepositExamineInfo(), start, limit);
		return list;
	}

	public int inDepositExamine(DepositExamineInfo info) {
		String sql = "INSERT INTO `examine_deposit` "
				+ "(`game_account`,`slice_value`,`slice_name`,`deposit_amount`,`deposit_time`,"
				+ "`deposit_channelvalue`,`deposit_channelname`,`out_bankaccount`,"
				+ "`income_account`,`status`,`order_number`,`order_create_time`,`content`)VALUES"
				+ "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		int i = jdbcTemplate.update(sql, info.getGameAccount(), info.getSliceValue(), info.getSliceName(),
				info.getDepositAmount(), info.getDepositTime(), info.getDepositChannelValue(),
				info.getDepositChannelName(), info.getOutbankAccount(), info.getIncomeAccount(), info.getStatus(),
				info.getOrderNumber(), info.getOrderCreateTime(), info.getContent());
		return i;
	}

	public DepositExamineInfo selectDepositExamineInfoById(int id) {
		String sql = "select * from examine_deposit where id=?";
		DepositExamineInfo info = jdbcTemplate.queryForObject(sql, new DepositExamineInfo(), id);
		return info;
	}

	public int updateDepositExamineInfoById(int[] ids, int type) {
		String sql = "update examine_deposit set status=? where id=?";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return ids.length;
				// 这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
			}

			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, type);
				ps.setInt(2, ids[i]);

			}
		});
		return 1;
	}

	public List<DepositExamineInfo> selectDepositExamineList(int[] ids) {
		String id = "";
		for (int i = 0; i < ids.length; i++) {
			id += ids[i] + ",";
		}
		id = id.substring(0, id.length() - 1);

		String sql = "select * from examine_deposit where id in(" + id + ")";

		List<DepositExamineInfo> list = jdbcTemplate.query(sql, new DepositExamineInfo());
		return list;
	}

	public PlayAccountInfo selectplaycount(String gameunique) {
		String sql = "select * from member_play_account where accountName=?";
		List<PlayAccountInfo> list = jdbcTemplate.query(sql, new PlayAccountInfo(),gameunique);
		return (list==null||list.size()<=0)?null:list.get(0);

	}

	public int selectbankuserCount(String cards) {
		String sql = "select count(*) from examine_deposit where make_collections in(" + cards + ")";

		Integer i = jdbcTemplate.queryForObject(sql, Integer.class);

		return i == null ? 0 : i;

	}

	/**
	 * 保存存款审核
	 * 
	 * @param info
	 * @author lijie1
	 * @date 2020/7/22 0022 17:30
	 * @return int
	 */
	public int saveDeposit(DepositExamineInfo info) {
		String sql = "INSERT INTO examine_deposit ( game_account,slice_value,slice_name,deposit_amount,deposit_time,deposit_channelvalue,"
				+ "deposit_channelname,out_bankaccount,outbank_peoplename,income_account,status,order_number,order_create_time,"
				+ "content,deposit_type,make_collections,discount_amount,game_unique_id) " + "VALUES"
				+ "( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )";
		int i = jdbcTemplate.update(sql, info.getGameAccount(), info.getSliceValue(), info.getSliceName(),
				info.getDepositAmount(), info.getDepositTime(), info.getDepositChannelValue(),
				info.getDepositChannelName(), info.getOutbankAccount(), info.getOutbankPeopleName(),
				info.getIncomeAccount(), info.getStatus(), info.getOrderNumber(), info.getOrderCreateTime(),
				info.getContent(), info.getDepositType(), info.getMakecollections(), info.getDiscountAmount(),
				info.getGameUniqueId());
		return i;
	}

	/**
	 * 查询订单是否存在
	 * 
	 * @param orderNumber
	 * @author lijie1
	 * @date 2020/7/22 0022 17:30
	 * @return int
	 */
	public int selectDepositByOrderNumber(String orderNumber) {
		String sql = "select count(*) from examine_deposit where order_number = ?";

		Integer i = jdbcTemplate.queryForObject(sql, Integer.class, orderNumber);

		return i == null ? 0 : i;
	}
}
